Worshop on ESD 4 NVE

title: “esd4nve_examples” author: “Abdelkader Mezghani” date: “March 11, 2016” output: html_document —

This is a generated R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.


Data & Structure

require(esd)
## Loading required package: esd
## Loading required package: ncdf4
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Attaching package: 'esd'
## The following object is masked from 'package:base':
## 
##     subset.matrix
## Display a list of the data included in the package
## data(package='esd')

R3 ESD & NVE

Find common locations and dates

it <- c("1960-01-01","2014-12-31") ## from previous diagnostics

is1 <- intersect(loc(t2m),loc(SD))
is2 <- intersect(is1,loc(pre))

pre.sub <- subset(pre,it=it,is=is2)
t2m.sub <- subset(t2m,it=it,is=is2)
SD.sub <- subset(SD,it=it,is=is2)

map(pre.sub,cex=1,col='grey30',bg='grey90',xlim=c(-5,35), ylim=c(55,71),add.text=TRUE)

Reading discharge data

## Create a function that converts NVE format to esd format
read.nve <- function(file='/data_esd_2016/atnsjo.csv',loc='Atnsjo',lat='61',lon='10',
                    alt=1204,stid = '2.32',plot=TRUE,...) {
            
  data <- read.csv(file=file,header=FALSE, comment.char="#", 
                 dec=".", sep=" ", stringsAsFactors=FALSE)

  dates <- as.Date(data$V1,format="%Y%m%d")
  xz <- zoo(data$V2,order.by=dates)
  
  y <- as.station(x=xz,loc=loc,lon=lon,lat=lat,alt=alt,stid=stid,
                   param="Q",
                   unit="m/s^{-2}",
                   longname="Discharge data",...)
  y[y==-9999] <- NA ## Replace missing values with NA   
  invisible(y)
}

Read meta data from the shape file

library(rgdal)
## Loading required package: sp
## rgdal: version: 1.2-8, (SVN revision 663)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 1.11.3, released 2015/09/16
##  Path to GDAL shared files: /usr/share/gdal/1.11
##  Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: 492]
##  Path to PROJ.4 shared files: (autodetected)
##  Linking to sp version: 1.2-5
library(leaflet)

rsf <- readOGR('Shape_filer_R3_WP4/R3_ESD_nbf_gruppe1.shp',layer = 'R3_ESD_nbf_gruppe1', GDAL1_integer64_policy = TRUE)
## OGR data source with driver: ESRI Shapefile 
## Source: "Shape_filer_R3_WP4/R3_ESD_nbf_gruppe1.shp", layer: "R3_ESD_nbf_gruppe1"
## with 18 features
## It has 12 fields
rsf.lonlat <- spTransform(rsf,CRSobj = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))

str(rsf)
## Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
##   ..@ data       :'data.frame':  18 obs. of  12 variables:
##   .. ..$ OBJECTID  : int [1:18] 135198 135242 135454 135497 135608 135699 135793 135810 136001 136109 ...
##   .. ..$ FELTNR    : int [1:18] 1762 2167 1396 861 1098 840 1136 1401 939 2230 ...
##   .. ..$ COMPOUND_K: Factor w/ 18 levels "000200032000",..: 14 17 12 7 9 6 10 13 8 18 ...
##   .. ..$ STASJON_NR: Factor w/ 18 levels "133.7.0","152.4.0",..: 1 7 17 9 14 6 15 18 12 13 ...
##   .. ..$ ST_NAVN   : Factor w/ 18 levels "Atnasjø","Bulken (Vangsvatnet)",..: 8 9 12 17 14 11 16 18 6 13 ...
##   .. ..$ AREAL_KM2 : num [1:18] 206 877 219 272 120 ...
##   .. ..$ AARTILSIG : num [1:18] 414 533 667 525 290 ...
##   .. ..$ MM_AAR    : num [1:18] 2012 608 3044 1929 2410 ...
##   .. ..$ AVR_6190  : num [1:18] 63.8 19.3 96.5 61.1 76.4 ...
##   .. ..$ MIDTILSIG : num [1:18] 13.1 16.9 21.1 16.6 9.2 ...
##   .. ..$ SHAPE_STAr: num [1:18] 2.06e+08 8.77e+08 2.19e+08 2.72e+08 1.21e+08 ...
##   .. ..$ SHAPE_STLe: num [1:18] 90307 206048 105129 124782 62956 ...
##   ..@ polygons   :List of 18
##   .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 273592 7087019
##   .. .. .. .. .. .. ..@ area   : num 2.06e+08
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:1035, 1:2] 283297 283493 283568 283672 283715 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 273592 7087019
##   .. .. .. ..@ ID       : chr "0"
##   .. .. .. ..@ area     : num 2.06e+08
##   .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 853352 7791408
##   .. .. .. .. .. .. ..@ area   : num 8.77e+08
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:1363, 1:2] 854400 854699 854923 855185 855380 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 853352 7791408
##   .. .. .. ..@ ID       : chr "1"
##   .. .. .. ..@ area     : num 8.77e+08
##   .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] -5879 6826471
##   .. .. .. .. .. .. ..@ area   : num 2.19e+08
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:1361, 1:2] -10037 -10050 -9954 -9888 -9935 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] -5879 6826471
##   .. .. .. ..@ ID       : chr "2"
##   .. .. .. ..@ area     : num 2.19e+08
##   .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 45710 6515539
##   .. .. .. .. .. .. ..@ area   : num 2.72e+08
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:3896, 1:2] 44064 44078 44079 44081 44102 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 45710 6515539
##   .. .. .. ..@ ID       : chr "3"
##   .. .. .. ..@ area     : num 2.72e+08
##   .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 41287 6677694
##   .. .. .. .. .. .. ..@ area   : num 1.21e+08
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:779, 1:2] 44619 44704 44772 44974 44980 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 41287 6677694
##   .. .. .. ..@ ID       : chr "4"
##   .. .. .. ..@ area     : num 1.21e+08
##   .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 67981 6513970
##   .. .. .. .. .. .. ..@ area   : num 1.82e+08
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:2675, 1:2] 64516 64550 64591 64610 64620 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 67981 6513970
##   .. .. .. ..@ ID       : chr "5"
##   .. .. .. ..@ area     : num 1.82e+08
##   .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] -22871 6724806
##   .. .. .. .. .. .. ..@ area   : num 50085661
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:470, 1:2] -17855 -17781 -17768 -17753 -17710 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] -22871 6724806
##   .. .. .. ..@ ID       : chr "6"
##   .. .. .. ..@ area     : num 50085661
##   .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 35880 6835407
##   .. .. .. .. .. .. ..@ area   : num 5.08e+08
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:2046, 1:2] 55244 55231 55217 55202 55197 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 35880 6835407
##   .. .. .. ..@ ID       : chr "7"
##   .. .. .. ..@ area     : num 5.08e+08
##   .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] -2360 6526583
##   .. .. .. .. .. .. ..@ area   : num 1.85e+08
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:2747, 1:2] 8670 8690 8721 8750 8779 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] -2360 6526583
##   .. .. .. ..@ ID       : chr "8"
##   .. .. .. ..@ area     : num 1.85e+08
##   .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 336425 6877014
##   .. .. .. .. .. .. ..@ area   : num 4.42e+09
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:4325, 1:2] 356050 356162 356354 356370 356501 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 336425 6877014
##   .. .. .. ..@ ID       : chr "9"
##   .. .. .. ..@ area     : num 4.42e+09
##   .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 432771 7311675
##   .. .. .. .. .. .. ..@ area   : num 5.26e+08
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:1312, 1:2] 439525 439561 439609 439659 439773 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 432771 7311675
##   .. .. .. ..@ ID       : chr "10"
##   .. .. .. ..@ area     : num 5.26e+08
##   .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 795142 7781416
##   .. .. .. .. .. .. ..@ area   : num 1.45e+08
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:726, 1:2] 801908 801941 801959 801997 802033 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 795142 7781416
##   .. .. .. ..@ ID       : chr "11"
##   .. .. .. ..@ area     : num 1.45e+08
##   .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 235024 6876321
##   .. .. .. .. .. .. ..@ area   : num 4.63e+08
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:924, 1:2] 236427 236400 236360 236343 236367 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 235024 6876321
##   .. .. .. ..@ ID       : chr "12"
##   .. .. .. ..@ area     : num 4.63e+08
##   .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 286955 6871237
##   .. .. .. .. .. .. ..@ area   : num 1.54e+10
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:10937, 1:2] 337402 337489 337549 337588 337662 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 286955 6871237
##   .. .. .. ..@ ID       : chr "13"
##   .. .. .. ..@ area     : num 1.54e+10
##   .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 183164 6899920
##   .. .. .. .. .. .. ..@ area   : num 1.83e+09
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:2560, 1:2] 192386 192547 192649 192846 192975 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 183164 6899920
##   .. .. .. ..@ ID       : chr "14"
##   .. .. .. ..@ area     : num 1.83e+09
##   .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 348096 6747173
##   .. .. .. .. .. .. ..@ area   : num 1.64e+09
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:3033, 1:2] 327345 327464 327546 327560 327631 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 348096 6747173
##   .. .. .. ..@ ID       : chr "15"
##   .. .. .. ..@ area     : num 1.64e+09
##   .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 179198 6862079
##   .. .. .. .. .. .. ..@ area   : num 1.12e+10
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:9370, 1:2] 192386 192547 192649 192846 192975 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 179198 6862079
##   .. .. .. ..@ ID       : chr "16"
##   .. .. .. ..@ area     : num 1.12e+10
##   .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 43884 6760472
##   .. .. .. .. .. .. ..@ area   : num 1.09e+09
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:3916, 1:2] 44614 44699 44778 44804 44850 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 43884 6760472
##   .. .. .. ..@ ID       : chr "17"
##   .. .. .. ..@ area     : num 1.09e+09
##   ..@ plotOrder  : int [1:18] 14 17 10 15 16 18 2 11 8 13 ...
##   ..@ bbox       : num [1:2, 1:2] -29393 6498836 869436 7816947
##   .. ..- attr(*, "dimnames")=List of 2
##   .. .. ..$ : chr [1:2] "x" "y"
##   .. .. ..$ : chr [1:2] "min" "max"
##   ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
##   .. .. ..@ projargs: chr "+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
df <- data.frame(rsf@data, stringsAsFactors = FALSE)

xy.coords <- as.data.frame(t(apply(as.matrix(1:18), 1, function(i) unlist(rsf@polygons[[i]]@labpt))))
colnames(xy.coords) <- c('x','y')
coordinates(xy.coords) <- ~ x + y
#CRS(xy.coords) <- rsf@proj4string
proj4string(xy.coords) <- rsf@proj4string

lonlat.coords <- spTransform(xy.coords,CRSobj = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))

lonlat <- attr(unclass(lonlat.coords),'coords')

meta <- data.frame(station_id = as.character(df$STASJON_NR), location = as.character(df$ST_NAVN), longitude = lonlat[1,] ,latitude = lonlat[,2])
## Warning in data.frame(station_id = as.character(df$STASJON_NR), location =
## as.character(df$ST_NAVN), : row names were found from a short variable and
## have been discarded
icons <- awesomeIcons(
  icon = 'ios-close',
  iconColor = 'black',
  library = 'ion',
  markerColor = 'orange')

leaflet(rsf.lonlat) %>% 
  addProviderTiles(providers$Esri.WorldStreetMap) %>%
  addPolygons(data = rsf.lonlat,stroke = TRUE, weight = 1) %>%
    addAwesomeMarkers(lng = lonlat[,1] , lat = lonlat[,2],icon = icons)

Reading data into esd format

library(esd) 
setwd('~/DATA/data_esd_2016/fwddataforr3workshopenom2uker')
lf <- list.files('R3_WP4_Qdata_sep2017',pattern = '.csv',full.names = TRUE) ## remember to update the file path name
readName <- function(lf) {
  namefromfile <- function(i) unlist(strsplit(basename(lf),split = '_')[[i]][2])
  fname <- apply(as.matrix(1:length(lf)),1,namefromfile)
  invisible(substr(fname,1,nchar(fname) - 4))
}

locs <- tolower(rsf$ST_NAVN)
staID <- substr(rsf$STASJON_NR,1,nchar(as.character(rsf$STASJON_NR))-2)
## Display list of discharge stations 
listStationName <- readName(lf)
for (i in 1 : length(lf)) {
  id <- grep(as.character(staID[i]), tolower(basename(lf)))
  ifile <- lf[id]
  cat(i, tolower(locs[i]),ifile,sep = '\n')
  #stopifnot(length(id) == 1)
  x <- read.nve(file= ifile,loc=locs[i],lon=lonlat[i,1],lat=lonlat[i,2], stid = staID[i], alt=NA , cntr='norway')
  if (i ==1) X <- x  else X <- combine.stations(X,x)
}
## 1
## krinsvatn (kringsvatnet)
## R3_WP4_Qdata_sep2017/133.7_Krinsvatn.csv
## 2
## lombola
## R3_WP4_Qdata_sep2017/223.2_Lombola.csv
## 3
## nautsundvatn
## R3_WP4_Qdata_sep2017/82.4_Nautsundvatn.csv
## 4
## tingvatn (lygne)
## R3_WP4_Qdata_sep2017/24.9_Tingvatn.csv
## 5
## reinsnosvatn
## R3_WP4_Qdata_sep2017/48.5_Reisnosvatn.csv
## 6
## myglevatn ndf.
## R3_WP4_Qdata_sep2017/22.16_Myglevatn.csv
## 7
## røykenes
## R3_WP4_Qdata_sep2017/55.4_RЫykenes.csv
## 8
## viksvatn (hestadfjorden)
## R3_WP4_Qdata_sep2017/83.2_Viksvatn.csv
## 9
## helleland
## R3_WP4_Qdata_sep2017/27.24_Helleland.csv
## 10
## nybergsund
## R3_WP4_Qdata_sep2017/311.6_Nybergsund.csv
## 11
## fustvatn
## R3_WP4_Qdata_sep2017/152.4_Fustvatn.csv
## 12
## halsnes
## R3_WP4_Qdata_sep2017/212.49_Halsnes.csv
## 13
## atnasjø
## R3_WP4_Qdata_sep2017/2.32_AtnasjЫ.csv
## 14
## elverum
## R3_WP4_Qdata_sep2017/2.604_Elverum.csv
## 15
## rosten
## R3_WP4_Qdata_sep2017/2.614_Rosten.csv
## 16
## knappom
## R3_WP4_Qdata_sep2017/2.142_Knappom.csv
## 17
## losna
## R3_WP4_Qdata_sep2017/2.145_Losna.csv
## 18
## bulken (vangsvatnet)
## R3_WP4_Qdata_sep2017/62.5_Bulken.csv
# Visual ckeck of the results
plot(X,plot.type = 'mult',ylab = toupper(substr(locs,1,5)),cex.lab = 0.8,new = FALSE)

# save to file 
q.nve <- X 
save(q.nve,file = 'R3_WP4_Qdata_sep2017/q.nve.rda')

Data Analysis

setwd('~/DATA/data_esd_2016/fwddataforr3workshopenom2uker/R3_WP4_Qdata_sep2017/')
load('q.nve.rda')

content <- paste(sep = "<br/>", paste("<b><a>",loc(q.nve),"</a></b>"))

leaflet() %>%
  addProviderTiles(providers$Esri.WorldStreetMap,
                   #addProviderTiles(providers$Stamen.TonerLite,
                   options = providerTileOptions(noWrap = TRUE)) %>%  
  addCircleMarkers(lng  = lon(q.nve), lat = lat(q.nve),radius = 6, weight = 0.9) %>%
  addPopups(lng  = lon(q.nve), lat = lat(q.nve), content,
            options = list(closeButton = FALSE,maxWidth = '50%')) 

plot(climatology(as.monthly(q.nve)),new=FALSE) ## Plot the climatology/hydrograph

# compute annual max
Qall.ann.max <- annual(q.nve,FUN="max")
plot(Qall.ann.max,plot.type = 'mult')

# perform a quick trend analysis
Qall.tr.ann.max <- apply(Qall.ann.max,2,FUN='trend.coef',na.rm=TRUE)
Qall.mean.ann.max <- apply(Qall.ann.max,2,FUN='mean',na.rm=TRUE)
tr.rel <- round(Qall.tr.ann.max / Qall.mean.ann.max *100,digits = 1) ## in % per decade

# test significancy
Qall.pval.ann.max <- round(apply(Qall.ann.max,2,FUN='trend.pval',na.rm=TRUE),digits = 2)

content <- paste(sep = "<br/>", paste("<b><a>",as.character(tr.rel)," % </a></b>"))

pal <- colorBin(rev(colscal(20,'t2m')),c(-10,10),bins = 10, pretty = TRUE)

df <- data.frame(lon = lon(q.nve),lat = lat(q.nve), trend = as.numeric(tr.rel))

# icons <- awesomeIcons(
#   icon = 'ios-close',
#   iconColor = 'black',
#   library = 'ion',
#   markerColor = pal(df$trend)
# )

# map the result
leaflet() %>% 
  addProviderTiles(providers$Esri.WorldStreetMap,
                   options = providerTileOptions(noWrap = TRUE)) %>%  
  addCircleMarkers(lng  = df$lon, lat = df$lat, radius = 10, fillOpacity = 0.8,
             stroke = TRUE, color = 'black', weight = 1, fill = TRUE, fillColor = pal(df$trend),
             popup = as.character(df$trend),label = as.character(df$trend)) %>%
  addLegend(position = 'bottomleft',pal = pal, values = pal(df$trend)) # %>%
  #addPopups(lng  = df$lon, lat = df$lat, content, options = list(closeButton = FALSE,maxWidth = '50%'))


## create a station obj.
tr.rel.as.station <- as.station(zoo(t(tr.rel),order.by=1))
## copy attributes from original data object
tr.rel.as.station <- attrcp(Qall.ann.max,tr.rel.as.station)
class(tr.rel.as.station) <- class(Qall.ann.max)
map.station(tr.rel.as.station,FUN='mean',colbar=list(breaks=seq(-5,5,1)),xlim=c(-5,35),ylim=c(55,71),new=FALSE)
title('Relative trend (in %/decade) in annual max of discharge data')

Downscaling single discharge data @ Atnasjø

load()
## Search the nearest meteorological station 
atn.nearby.pre <- nearest(pre.sub,is=atn)
atn.nearby.t2m <- nearest(t2m.sub,is=atn)
atn.nearby.SD <- nearest(SD.sub,is=atn)

## Display the nearest location name
loc(atn.nearby.pre)

## 
map(atn,cex=1,pch=21,xlim=c(-5,35), ylim=c(55,71),add.text=TRUE)
points(lon(pre.sub),lat(pre.sub),pch=21,col='grey20',bg='grey90',xlim=c(-5,35), ylim=c(55,71))                 
points(lon(atn.nearby.pre),lat(atn.nearby.pre),pch=20,col='red',bg='white',xlim=c(-5,35), ylim=c(55,71))